<?php
 
function list_fasilitas_perumahan(){
	my_set_code_js('
		function resetGajiConfirm(kid){
			var t = confirm(\'Data gaji akan di reset?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=resetgaji\';
			}
			return false;
		}
		
		function ReUse(kid){
			var t = confirm(\'Import data periode sebelumnya?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=reuse&\';
			}
			return false;
		}
		
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');
	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'text-align:left;border-bottom:2px solid;width:35%'),  
		'Jabatan'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	if(isset($_GET['key']))
	$query = "SELECT  a.fas_rumah_id, a.karyawan_id , a.nominal FROM pr_perumahan a
				INNER JOIN   karyawan c  ON a.karyawan_id = c.karyawan_id
		INNER JOIN biodata d ON d.biodata_id = c.biodata_id
		WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
		AND ( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' )
		  ORDER BY c.nik ASC
		";
	else
	$query = " SELECT   a.fas_rumah_id,a.karyawan_id , a.nominal FROM pr_perumahan a
				INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id 
				 
				";
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =7;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		  
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		if(empty($karyawan['gelar'])){
			$nama_karyawan=ucfirst($karyawan['nama']) ;
		}else{
			$nama_karyawan=trim(ucfirst($karyawan['nama'])).", ".$karyawan['gelar'] ;
		}

		$jabatan = my_get_data_by_id('jabatan' ,'jabatan_id' , ($karyawan['jabatan_id']<>'' ? $karyawan['jabatan_id'] : 0 ));
		
		$editproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=edit&id='.$ey['fas_rumah_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		 
		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['fas_rumah_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		$row[] = array(
			'nik'=>position_text_align ($karyawan['nik'] ,'center'),    
			'gd'=> $nama_karyawan ,      
			'jabatan'=> $jabatan['nama_jabatan']   ,
			'skr'=>position_text_align('Rp. '.rp_format($ey['nominal']) , 'right')  ,
			'operasi'=> position_text_align( $edit_button .' '.$delete_button, 'right'),
		); 
	}
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();	  
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span>
	<input type="hidden" name="task" value="'.$_GET['task'].'"/> 
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';

	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .='<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	$view .= form_field_display( '<span class="label_form"><b>'.ucfirst($komponen_gajis['nama_komponen_gaji']).'</b></span>', "<b>Komponen Gaji</b>"    ); 
	
	//$button_import =   ($komponen_gajis['reset_tutup_bulan'] =='yes') ? '';
	$button_import =  '<input type="button" value="Re-Use" onclick="javascript:ReUse('.$komponen_gaji_id.')" class="main_button" /> ' ;
	$view .= '<tr><td><br/>'.$search .'</td><td valign="bottom"><div style="width:100%;text-align:right;">
	'.$button_import .'
	<input type="button" value="Tambah Data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=pr_upload_gaji&task=edit&komponen_gaji_id='.$komponen_gaji_id.'&id=0\' });" class="main_button" /> 
	<input type="button" value="Upload" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=upload&id=0\' });" class="main_button" /> 
	</td></tr>'   ; 
			return  $view.   table_builder($header , $datas ,  6 , false ,$paging );	

} 


function page_submit_perumahan($id = 0){

	
	$karyawan_id =check_karyawan_by_nik(sprintf('%07d',$data[0]));
	$nominal = $_POST['nominal'];

	return stor_table_perumahan($karyawan_id,$nominal);
}

function upload_form_page_submit(){
	$dest ='files/csv/exception_perumahan_'.date('Ymd_his').'.csv';
	$ori = $_FILES['file']['tmp_name'];
	$upload = move_uploaded_file($ori , $dest); 
	if($upload){
		$handle = fopen($dest, "r"); 
		my_query("	TRUNCATE TABLE `pr_perumahan`	");
		while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
			if(trim($data[0])<>'' && $data[1]<> ''){
				$karyawan_id =check_karyawan_by_nik(sprintf('%07d',$data[0]));
				stor_table_perumahan($karyawan_id ,$data[1] );
			}
		}
		fclose($handle);
	}
	return $dest ;
}

function upload_form_validate(){
	$errsubmit = false;
	$err = array();
	
	$exts = array('csv','xls');
	$ext = end(explode("." , strtolower( $_FILES['file']['name'] ) ) );
	if( ! is_file($_FILES['file']['tmp_name']) ){
		$errsubmit = true;
		$err[] = "Tidak ada file yang disertakan";
	}
	elseif( ! in_array($ext , $exts)  ){
		$errsubmit = true;
		$err[] = "File tidak dikenali";
	}	
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function upload_form_page(){
	$view = form_header( "upload" , "upload"  );
	
	$file = array(
			'name'=>'file',
			'value'=>(isset($_POST['file'])? $_POST['file'] : $fields['file']),
			'id'=>'file' ,'size'=>'70',
			'type'=>'file'
	);
	$form_nominal = form_dynamic($file);
	$view .= form_field_display( $form_nominal , "File CSV" );
	
	$submit = array(
		'value' => ' Proses ',
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function stor_table_perumahan($karyawan_id,$nominal){

	$datas = array(
		'karyawan_id'	=> my_type_data_int($karyawan_id),	
		'nominal'		=>	my_type_data_str( $nominal),
	);
	return my_insert_record('pr_perumahan' , $datas);
}


function form_fas_rumah_create( $id=0){ 
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
			'components/system/js/calendar/calendarDateInput.js'
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');

	$view = form_header( "upload" , "upload"  ); 
 	$fields = my_get_data_by_id('pr_perumahan','fas_rumah_id',$id);
	$karyawan = loaddata_karyawan($fields['karyawan_id']);
	$nama_karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>( isset($_POST['nama_karyawan'] ) ? $_POST['nama_karyawan'] : $karyawan['nik'].($fields ? '/' : '').$karyawan['nama']  ),
		'id'=>'nama_karyawan' , 
		'type'=>'text',
		'size'=>'45' ,
		'onchange'=>'javascript:checkName(this.value)'
	);
	
	$form_field_nama_karyawan = form_dynamic($nama_karyawan); 
	$view .= form_field_display( $form_field_nama_karyawan  , "NIK/Nama Karyawan"    );
	
	$nominal = array(
			'name'=>'nominal',
			'value'=>(isset($_POST['nominal'])? $_POST['nominal'] : $fields['nominal']),
			'id'=>'nominal' 
	);
	$form_nominal = form_money($nominal);
	$view .= form_field_display( $form_nominal , "Nominal" );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}